package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.DateUtils;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysDictDataDO;
import com.andy.blog.dal.mapper.SysDictDataMapper;
import com.andy.blog.dal.service.SysDictDataDalService;
import com.andy.blog.dal.vo.SysDictDataVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典 业务层处理
 * <p>
 * Created by andy on 2023-04-05 11:38
 */
@Service
public class SysDictDataDalServiceImpl implements SysDictDataDalService {

    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysDictDataDO> buildQueryWrapper(SysDictDataVO sysDictDataVO) {
        LambdaQueryWrapper<SysDictDataDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(StringUtils.isNotBlank(sysDictDataVO.getDictType()), SysDictDataDO::getDictType, sysDictDataVO.getDictType())
                .like(StringUtils.isNotBlank(sysDictDataVO.getDictLabel()), SysDictDataDO::getDictLabel, sysDictDataVO.getDictLabel())
                .eq(StringUtils.isNotBlank(sysDictDataVO.getStatus()), SysDictDataDO::getStatus, sysDictDataVO.getStatus())
                .orderByAsc(SysDictDataDO::getDictSort);

        return lambdaQueryWrapper;
    }

    @Override
    public IPage<SysDictDataDO> selectListByPage(SysDictDataVO sysDictDataVO) {

        LambdaQueryWrapper<SysDictDataDO> lambdaQueryWrapper = buildQueryWrapper(sysDictDataVO);

        IPage<SysDictDataDO> page = new Page<>(sysDictDataVO.getPageNum(), sysDictDataVO.getPageSize());

        return dictDataMapper.selectPage(page, lambdaQueryWrapper);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }


    /**
     * 新增保存字典数据信息
     *
     * @param sysDictDataVO 字典数据信息
     * @return 结果
     */
    @Override
    public void insertDictData(SysDictDataVO sysDictDataVO) {
        SysDictDataDO dictDataDO = TransformUtils.transform(sysDictDataVO, SysDictDataDO.class);

        dictDataDO.setCreateTime(DateUtils.getCurrentDate());
        dictDataDO.setCreateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = dictDataMapper.insert(dictDataDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public void updateDictData(SysDictDataVO sysDictDataVO) {

        SysDictDataDO sysDictDataDO = TransformUtils.transform(sysDictDataVO, SysDictDataDO.class);

        sysDictDataDO.setUpdateTime(DateUtils.getCurrentDate());
        sysDictDataDO.setUpdateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = dictDataMapper.updateById(sysDictDataDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101);
        }
    }

    @Override
    public List<SysDictDataDO> selectListByCondition(SysDictDataVO sysDictDataVO) {
        LambdaQueryWrapper<SysDictDataDO> lambdaQueryWrapper = buildQueryWrapper(sysDictDataVO);
        return dictDataMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SysDictDataDO selectById(Long id) {
        return dictDataMapper.selectById(id);
    }

    @Override
    public void deleteById(Long id) {
        dictDataMapper.deleteById(id);
    }

    @Override
    public List<SysDictDataDO> selectDictDataByType(String dictType) {
        LambdaQueryWrapper<SysDictDataDO> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictDataDO>();

        lambdaQueryWrapper.eq(SysDictDataDO::getStatus, "0")
                .eq(StringUtils.isNoneBlank(dictType), SysDictDataDO::getDictType, dictType)
                .orderByAsc(SysDictDataDO::getDictSort);
        return dictDataMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public int countDictDataByType(String dictType) {
        return dictDataMapper.countDictDataByType(dictType);
    }

    @Override
    public void updateDictDataType(String oldDictType, String newDictType) {
        dictDataMapper.updateDictDataType(oldDictType, newDictType);
    }
}
